package com.loobot.album.service.impl;

import com.github.pagehelper.PageHelper;
import com.loobot.album.dao.mapper.AlbumMapper;
import com.loobot.album.dao.model.Album;
import com.loobot.album.exception.AppException;
import com.loobot.album.net.ErrorCode;
import com.loobot.album.net.param.request.GetAlbum;
import com.loobot.album.net.param.request.GetPictures;
import com.loobot.album.service.AlbumService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

/**
 * @author: Ike.Fan
 * @date: 2019/3/14.
 */
@Service
public class AlbumServiceImpl implements AlbumService {
    @Resource
    AlbumMapper mapper;
    @Override
    public String addAlbum(Album album) {
        if(album.getCreateTime()==null){
            album.setCreateTime(Calendar.getInstance().getTime());
        }
        if(mapper.selectByOwnerId(album.getOwnerId())!=null){
            throw new AppException(ErrorCode.ALREADY_EXIST);
        }
        mapper.insertSelective(album );
        return album.getId();
    }

    @Override
    public int deletePic(String picUrl) {
        return 0;
    }

    @Override
    public int updateAlbum(Album albumParam) {
        return mapper.updateByPrimaryKeySelective(albumParam);
    }

    @Override
    public int delete(String uuid) {
        return mapper.deleteByPrimaryKey(uuid);
    }

    @Override
    public Album getAlbumByOwnerId(String ownerId) {
        return mapper.selectByOwnerId(ownerId);
    }

    @Override
    public List<Album> getAlbums(GetAlbum getAlbum) {
        PageHelper.startPage(getAlbum.getPageNum(), getAlbum.getPageSize());
        return mapper.selectAll();
    }
}
